package com.beans;

import creator.ui.graphics.Magicanvas;
import creator.ui.graphics.bean.Bean;
import java.awt.Desktop;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class HrefBean extends Bean {

  private Image href;
  private int hrefX;
  private int hrefY;
  private int hrefW;
  private int hrefH;
  private int hrefOldY;

  public void initBean(Magicanvas cvs) {
    this.href = getImage("/res/img/href.gif");
    trackerImage(this.href, cvs);
    this.hrefW = this.href.getWidth(cvs);
    this.hrefH = this.href.getHeight(cvs);
    this.hrefX = (172 - this.hrefW / 2);
    this.hrefY = (int) (614.0D - RoadBean.getRoadH() * 2.5D);
    this.hrefOldY = this.hrefY;
    addListener(cvs);
  }

  private void addListener(Magicanvas cvs) {
    HrefBean hrefBean = this;
    cvs.addMouseListener(
      new MouseAdapter(hrefBean) {
        public void mousePressed(MouseEvent e) {
          if (
            (e.getX() > HrefBean.this.hrefX) &&
            (e.getX() < HrefBean.this.hrefX + HrefBean.this.hrefW) &&
            (e.getY() > this.val$hrefBean.getHrefY()) &&
            (e.getY() < this.val$hrefBean.getHrefY() + HrefBean.this.hrefH)
          ) this.val$hrefBean.setHrefY(HrefBean.this.hrefOldY + 3);
        }

        public void mouseReleased(MouseEvent e) {
          if (
            (e.getX() > HrefBean.this.hrefX) &&
            (e.getX() < HrefBean.this.hrefX + HrefBean.this.hrefW) &&
            (e.getY() > this.val$hrefBean.getHrefY()) &&
            (e.getY() < this.val$hrefBean.getHrefY() + HrefBean.this.hrefH)
          ) {
            this.val$hrefBean.setHrefY(HrefBean.this.hrefOldY);
            ButtonEvent.sendMassage("-> Clicked href");
            try {
              openHtml();
            } catch (IOException e1) {
              e1.printStackTrace();
            } catch (URISyntaxException e1) {
              e1.printStackTrace();
            }
          }
        }

        private void openHtml() throws IOException, URISyntaxException {
          String uri = getClass()
            .getResource("/res/html/Euscen.htm")
            .getPath()
            .replace("file:/", "")
            .replace("FlappyBird.jar!/", "")
            .substring(1);
          Desktop.getDesktop().browse(new URI(uri));
        }
      }
    );
  }

  public Image getHref() {
    return this.href;
  }

  public int getHrefX() {
    return this.hrefX;
  }

  public void setHrefX(int hrefX) {
    this.hrefX = hrefX;
  }

  public int getHrefY() {
    return this.hrefY;
  }

  public void setHrefY(int hrefY) {
    this.hrefY = hrefY;
  }

  public int getHrefW() {
    return this.hrefW;
  }

  public void setHrefW(int hrefW) {
    this.hrefW = hrefW;
  }

  public int getHrefH() {
    return this.hrefH;
  }

  public void setHrefH(int hrefH) {
    this.hrefH = hrefH;
  }
}
